package com.yanqu.road.server.manger.systemgroup;

import com.google.protobuf.AbstractMessage;
import com.yanqu.road.entity.enums.eSystemId;
import com.yanqu.road.entity.player.UserInfo;
import com.yanqu.road.entity.systemgroup.common.SystemServerGroupWeekData;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.pb.union.SystemGroupMatchProto;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.manger.plugin.ManagerReloadListener;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.utils.date.DateHelper;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossSystemGroupMgr extends TempMgr {

    //systemId,weekId
    private static Map<Integer, Map<Integer, SystemServerGroupWeekData>> systemGroupMap = new ConcurrentHashMap<>();

    private static List<ManagerReloadListener> groupChangeListener = new ArrayList<>();

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    public static void syncMatchGroup(int weekId, int systemId, SystemServerGroupWeekData weekData) {
        if(!systemGroupMap.containsKey(systemId)){
            systemGroupMap.put(systemId, new ConcurrentHashMap<>());
        }
        SystemServerGroupWeekData old = systemGroupMap.get(systemId).get(weekId);
        systemGroupMap.get(systemId).put(weekId, weekData);
        if(old == null || (weekData.getGroupId() != old.getGroupId() || weekData.getServerIdList().size() != old.getServerIdList().size())){
            notifyReload();
        }
    }

    private static void notifyReload(){
        for (ManagerReloadListener listener : groupChangeListener) {
            listener.notifyReload();
        }
    }

    public static void addReloadListener(ManagerReloadListener reloadListener){
        groupChangeListener.add(reloadListener);
    }

    public static SystemServerGroupWeekData getSystemServerGroup(int weekId, int systemId){
        if(!systemGroupMap.containsKey(systemId)){
            return null;
        }
        return systemGroupMap.get(systemId).get(weekId);
    }

    /**
     * 发送到活动跨服
     */
    public static void sendAppointActivityPacket(long playerId, int code, int systemId, AbstractMessage.Builder<?> messageBuilder) {
        int weekId = GameConfig.getSystemThisWeekId();
        SystemServerGroupWeekData groupWeekData = getSystemServerGroup(weekId, systemId);
        if (groupWeekData == null) {
            getLogger().info("系统{}， 本周{} 无分组", systemId, weekId);
            return;
        }
        GamePlayerMgr.sendAppointActivityPacket(code, messageBuilder, playerId, groupWeekData.getCrossId(), groupWeekData.getGroupId());
    }

    public static void uploadTodayServerUserAbility(int systemId){
        if(systemId == eSystemId.NewMonopolyMarket.getValue()){
            //活跃天数
            int activeDay = GameConfig.NEWMONOPOLYMARKET_RESOURCE_PARAM_3;

            int zeroTime = DateHelper.getTodayZeroTime();
            List<UserInfo> list = UserMgr.getUserInfoList();
            //活动开始3天内有登录过的
            list.removeIf(userInfo -> userInfo.getLastLoginTime() < zeroTime - activeDay * DateHelper.DAY_SECONDS);

            BigInteger earn = BigInteger.ZERO;
            for (UserInfo userInfo : list) {
                earn = earn.add(userInfo.getEarnSpeed());
            }

            SystemGroupMatchProto.ServerUploadAbilityMsg.Builder upload = SystemGroupMatchProto.ServerUploadAbilityMsg.newBuilder();
            upload.setSystemId(systemId);
            upload.setAbility(earn.toString());
            GamePlayerMgr.sendPacket(0, YanQuMessageUtils.buildMessage(Protocol.C3_CROSS_SYSTEM_GROUP_MATCH_UPLOAD_SEVER_ABILITY, upload));
        }
    }
}
